package 链表;

public class LinkedList {

    Node first;
    Node last;
    int size = 0;


    public LinkedList() {

    }

    public LinkedList(String s) {
        Node newNode = new Node(null, null, s);
        first = newNode;
        last = newNode;
        size++;
    }


    public void add(String s) {  
        Node newNode = new Node(null, null, s);
        if (size == 0) {
            first = newNode;
            last = newNode;
        } else if (size == 1) {
            last = newNode;
            first.next = last;
            last.perv = first;
        } else {
            Node temp = last;
            last = newNode;
            temp.next = last;
            last.perv = temp;
        }
        size++;
    }

    public void firstAdd(String s) {
        Node newNode = new Node(null, null, s);
        newNode.next = first;
        first.perv = newNode;
        first = newNode;
        size++;
    }

    public void lastAdd(String s) {
        Node newNode = new Node(null, null, s);
        newNode.perv = last;
        last.next = newNode;
        last = newNode;
        size++;
    }


    

    @Override
    public String toString() {
        Node node = first;
        System.out.print("{");
        for (int i = 1; i <= size; i++) {
            System.out.print(node.s);
            System.out.print(", ");
            node = node.next;
        }
        System.out.print("}");
        return null;
    }

   
}

class Node{
	 Node perv;
        	 Node next;
        	 String s;

        	public Node(Node perv, Node next, String s) {
            		this.perv = perv;
            		this.next = next;
            		this.s = s;
        	}
}
